<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:54
 */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\SystemConfigServices;
use Illuminate\Http\Request;
use App\Lib\Response;


class RoomHostMicroController extends Controller
{
    private $services_room_hostmicro;
    private $services_room_msg;
    public function __construct()
    {
        $this->services_room_hostmicro = RoomServicesInit::createHostMicro();
        $this->services_room_msg = new RoomMsgServices();
    }

    /**
     * 主持麦上麦
     * @param Request $request
     */
    public function into(Request $request){
        if (!$request->get("room_id")){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $room_id = $request->get("room_id");
        $user_id = null;

        $data = $this->services_room_hostmicro->into($room_id, $user_id);
        if($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }

        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        if ($data === Hint::NOT_ALLOWED){
            Response::returnResult(Hint::NOT_ALLOWED);
            return;
        }
        if ($data === Hint::ROOM_MICRO_NOT_NULL){
            Response::returnResult(Hint::ROOM_MICRO_NOT_NULL);
            return;
        }
        if ($data === Hint::ROOM_WAITBIT_IS_EXIST){
            Response::returnResult(Hint::ROOM_WAITBIT_IS_EXIST);
            return;
        }
        if ($data === Hint::ROOM_MICRO_USER_IS_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_USER_IS_EXIST);
            return;
        }
        if ($data === Hint::ROOM_USER_NOT_EXIST){
            Response::returnResult(Hint::ROOM_USER_NOT_EXIST);
            return;
        }
        $this->services_room_msg->command_hostmicro_into($room_id, 0, $user_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 主持麦信息
     * @param Request $request
     */
    public function info(Request $request){
        if (!$request->get("room_id")){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $room_id = $request->get("room_id");
        $data = $this->services_room_hostmicro->get_micro($room_id);
        if ($data == Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        Response::returnResult(Hint::SUCCESS, $data);
    }


    /**
     * 主持麦下麦
     * @param Request $request
     */
    public function out(Request $request){
        if (!$request->get("room_id")){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $room_id = $request->get("room_id");
        $user_id = null;
        $data = $this->services_room_hostmicro->out($room_id, $user_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::NOT_ALLOWED){
            Response::returnResult(Hint::NOT_ALLOWED);
        }
        if ($data === Hint::ROOM_USER_NOT_EXIST){
            Response::returnResult(Hint::ROOM_USER_NOT_EXIST);
        }
        $this->services_room_msg->command_hostmicro_out($room_id, 0, $user_id);
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 主持麦闭麦
     * @param Request $request
     */
    public function volume_close(Request $request){
        if (!$request->get("room_id")){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $room_id = $request->get("room_id");
        $data = $this->services_room_hostmicro->volume_close($room_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        $this->services_room_msg->command_hostmicro_volume_close($room_id, 0);
        $this->services_room_msg->broadcast_hostmicro_volume_close($room_id, 0);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 主持麦开麦
     * @param Request $request
     */
    public function volume_open(Request $request){
        //增加限制房间开启
        $roomAndVolumeCloseConfig = (new SystemConfigServices())->getRoomAndVolumeCloseConfig();
        $currentTime = time();
        if ($roomAndVolumeCloseConfig[2] > 0 && $roomAndVolumeCloseConfig[3] > 0
            && $currentTime > $roomAndVolumeCloseConfig[2] && $currentTime < $roomAndVolumeCloseConfig[3]){
            Response::returnResult(Hint::ERR_SYS_FAIL, null, '维护中，请耐心等待');
        }

        if (!$request->get("room_id")){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $room_id = $request->get("room_id");
        $data = $this->services_room_hostmicro->volume_open($room_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }

        if ($data === Hint::USER_MICRO_IS_BLOCK){
            Response::returnResult(Hint::USER_MICRO_IS_BLOCK);
        }

        $this->services_room_msg->command_hostmicro_volume_open($room_id, 0);
        $this->services_room_msg->broadcast_hostmicro_volume_open($room_id, 0);
        Response::returnResult(Hint::SUCCESS);
    }


}